//
//  ViewController.m
//  04_UIControl
//
//  Created by 徐昊 on 16/7/19.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor blackColor];
#pragma mark - UISwitch
    // Switch 宽和高不影响控件的大小, 只有x和y
    UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    [self.view addSubview:switchControl];
//    switchController.backgroundColor = [UIColor redColor];
    // 1.开启时的控件颜色
    switchControl.onTintColor = [UIColor blackColor];
    // 2.边框及切换时到关闭时的颜色(null_resettable
    switchControl.tintColor = [UIColor yellowColor];
    // 3.开关按钮(圆圈)的颜色
    switchControl.thumbTintColor = [UIColor orangeColor];
    // 4.打开时的图片, iOS7后失效
    switchControl.onImage = [UIImage imageNamed:@"1"];
    // 5.关闭时的图片, iOS7后失效
    switchControl.offImage = [UIImage imageNamed:@"2"];
    // 6.判断switchControl的开闭状态 NO为关闭 YES为开启
    // 通过setter方法, 改变switchControl的状态
    switchControl.on = YES;
    NSLog(@"%@", switchControl.isOn ? @"YES" : @"NO");
    [switchControl setOn:YES animated:YES];
    // 7.添加绑定事件(与Button相同)
    [switchControl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
#pragma mark - UISlider
    // 高度不影响视图的高度, 这个控件永远居中于视图的中间
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 200, 400, 20)];
    [self.view addSubview:slider];
    // 最大值和最小值
    slider.minimumValue = 0;
    slider.maximumValue = 1.0;
    // 最大值和最小值的图片
    slider.minimumValueImage = [UIImage imageNamed:@"1"];
    slider.maximumValueImage = [UIImage imageNamed:@"2"];
    // 滑动按钮的颜色
    slider.thumbTintColor = [UIColor orangeColor];
    // 设置滑块的图片
    [slider setThumbImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
    // continuous (在动作结束时才触发动作)
    slider.continuous = NO;
    // 绑定事件
    [slider addTarget:self action:@selector(slideAction:) forControlEvents:UIControlEventValueChanged];
    // 设置划过区域的进度条的颜色
    [slider setMinimumTrackTintColor:[UIColor redColor]];
    // 设置未划过区域的进度条的颜色
    [slider setMaximumTrackTintColor: [UIColor blackColor]];
    // 设置划过的区域的进度条的背景图片
    [slider setMinimumTrackImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    // 设置未划过区域的进度条的背景图片
    [slider setMaximumTrackImage:[UIImage imageNamed:@"4"] forState:UIControlStateNormal];
#pragma mark - UISegmentControl
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"first", @"second"]];
    // segment不给frame, 系统会自动计算大小(推荐: 加上frame)
    NSLog(@"%@", NSStringFromCGRect(segment.frame));
    segment.frame = CGRectMake(20, 20, 335, 50);
    [self.view addSubview:segment];
    // 1. momentary 设置为YES, 如果点击选中没有选中的效果
    // 默认为 NO
    segment.momentary = YES;
    // 2. numberOfSegments 显示当前分段控制器的段数(只读)
    NSUInteger numberOfSegments = segment.numberOfSegments;
    NSLog(@"分段控制器的段数%lu", numberOfSegments);
    // 使用标题添加段数
    [segment insertSegmentWithTitle:@"third" atIndex:1 animated:YES];
    // 使用图片添加标题
    [segment insertSegmentWithImage:[UIImage imageNamed:@"1"] atIndex:2 animated:YES];
    segment.tag = 1000;
    // 修改对应下标的名字
    [segment setTitle:@"xuhao" forSegmentAtIndex:1];
    // width默认是0.0, 修改制定下标的宽度
    [segment setWidth:0 forSegmentAtIndex:0];
    // 设置对应下标内容的偏移
    [segment setContentOffset:CGSizeMake(0, 0) forSegmentAtIndex:0];
    // 下标取出来的东西是否能被选中, 默认YES
    [segment setEnabled:NO forSegmentAtIndex:0];
    // 改变segment的文字及边框的颜色
    segment.tintColor = [UIColor yellowColor];
    // selectedSegmentWidthByContent 选中的下标
    segment.selectedSegmentIndex = 1;
    // apportionsSegmentWidthsByContent 默认为0 如果分段控制器的段宽(setWidth方法设置)为0时 并且这个属性设为yes, 系统会自动计算
    segment.apportionsSegmentWidthsByContent = YES;
    
    // 练习:
    // 点击Button, 是Segment添加新的标题
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 100, 100, 50);
    button.backgroundColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
#pragma mark - UIPageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, 300, 400, 50)];
    [self.view addSubview:pageControl];
    // PageControl的背景颜色设置
    pageControl.backgroundColor = [UIColor redColor];
    // 设置总页数
    pageControl.numberOfPages = 10;
    // 当前页的设置
    pageControl.currentPage = 5;
    // 当只有一页的时候隐藏
    pageControl.hidesForSinglePage = YES;
    NSLog(@"%ld", pageControl.numberOfPages);
    pageControl.pageIndicatorTintColor = [UIColor greenColor];
    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    CGSize size = [pageControl sizeForNumberOfPages:0];
    NSLog(@"%@", NSStringFromCGSize(size));
    [pageControl addTarget:self action:@selector(pageControlChange:) forControlEvents:UIControlEventValueChanged];
    // 关闭系统事件配置的切换, 直到调用updatePageDisplay的方法才会显示(默认为NO, 系统会自动管理)
    pageControl.defersCurrentPageDisplay = YES;
}
- (void)switchAction:(UISwitch *)switchControl {
    // 根据switch的状态, 决定触发什么样的事件
    if (switchControl.on == YES) {
        NSLog(@"开");
    } else if (switchControl.on == NO) {
        NSLog(@"关");
    }
}
- (void)slideAction:(UISlider *)slide {
    NSLog(@"滑动");
}
- (void)add:(UIButton *)button {
    UISegmentedControl * segment = [self.view viewWithTag:1000];
    [segment insertSegmentWithTitle:@"添加标题" atIndex:2 animated:YES];
}
- (void)pageControlChange:(UIPageControl *)pageControl {
    [pageControl updateCurrentPageDisplay];
    NSLog(@"Change");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
